package com.sys.ai.controller;

import com.sys.ai.domain.AiKnowledge;
import com.sys.ai.service.IAiKnowledgeService;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库Controller
 *
 * @author LGY
 * @date 2025-05-16
 **/
@Tag(name = "知识库")
@RestController
@RequestMapping("/ai/knowledge")
public class AiKnowledgeController extends BaseController {
    @Autowired
    private IAiKnowledgeService aiKnowledgeService;

    /**
     * 查询知识库列表
     */
    @PostMapping("/list")
    @Operation(summary = "查询知识库分页列表")
    public TableDataInfo list(@RequestBody AiKnowledge aiKnowledge) {
        startPage(aiKnowledge);
        List<AiKnowledge> list = aiKnowledgeService.selectAiKnowledgeList(aiKnowledge);
        return getDataTable(list);
    }

    @Operation(summary = "查询全部知识库列表")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        return success(aiKnowledgeService.list());
    }

    @GetMapping("/listAllComplete")
    @Operation(summary = "查询全部已完成的知识库列表")
    public AjaxResult listAllComplete() {
        List<AiKnowledge> list = aiKnowledgeService.list();
        return success(list);
    }

    /**
     * 导出知识库列表
     */
    @Log(title = "知识库", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出知识库")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiKnowledge aiKnowledge) {
        List<AiKnowledge> list = aiKnowledgeService.selectAiKnowledgeList(aiKnowledge);
        ExcelUtil<AiKnowledge> util = new ExcelUtil<AiKnowledge>(AiKnowledge.class);
        util.exportExcel(response, list, "知识库数据");
    }

    /**
     * 获取知识库详细信息
     */
    @Operation(summary = "查询知识库详情")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(aiKnowledgeService.selectAiKnowledgeById(id));
    }

    /**
     * 新增知识库
     */
    @Log(title = "知识库", businessType = BusinessType.INSERT)
    @Operation(summary = "新增知识库")
    @PostMapping("/save")
    public AjaxResult add(@RequestBody AiKnowledge aiKnowledge) {
        aiKnowledgeService.insertAiKnowledge(aiKnowledge);
        return success();
    }

    /**
     * 修改知识库
     */
    @Log(title = "知识库", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改知识库")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody AiKnowledge aiKnowledge) {
        aiKnowledgeService.updateAiKnowledge(aiKnowledge);
        return success();
    }

    /**
     * 删除知识库
     */
    @Operation(summary = "删除知识库")
    @Log(title = "知识库", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        aiKnowledgeService.deleteAiKnowledgeByIds(ids);
        return success();
    }
}
